package com.example.onlinemarket.graphics;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageButton;

import com.bumptech.glide.Glide;
import com.example.onlinemarket.R;
import com.example.onlinemarket.fragment.ShoppingCartFragment;

public class AddButton extends AppCompatImageButton {

    private int goodId;
    private double goodPrice;
    private TextView numTv;
    private ReduceButton reduceButton;
    private ShoppingCartFragment.OnTotalPriceChangedListener listener;

    public AddButton(Context context) {
        super(context);
        setAddImg(context);
    }

    public AddButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setAddImg(context);
    }

    public AddButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAddImg(context);
    }

    //设置数量文本及点击监听器
    public void setNumTv(int goodId, double goodPrice, TextView numTv, ReduceButton reduceButton, ShoppingCartFragment.OnTotalPriceChangedListener listener){
        this.goodId = goodId;
        this.goodPrice = goodPrice;
        this.numTv = numTv;
        this.reduceButton = reduceButton;
        this.listener = listener;
        setOnClickListener(v -> {
            int num = Integer.parseInt(numTv.getText().toString());
            if(num==0)
                reduceButton.setVisibility(VISIBLE);
            numTv.setText(String.valueOf(num+1));
            listener.updateGoodNum(goodId, 1);
            listener.getTotalPrice(goodPrice);
        });
    }

    //设置图标
    private void setAddImg(Context context){
        Glide.with(context).load(R.drawable.ic_add).into(this);
    }
}
